################################################################################
#####################  distennuated correlation PGY 2  #########################
################################################################################
library(psych)
PGY2_C <- matrix(ncol = 6, nrow = 8)
for (n in 1:8)
{
  ###setwd("C:/XXXX")
  
  dat_faculty_autonomy <- read.csv(paste("PGY2_", n,".csv",sep=""))%>%
    filter(rater=="1")%>%
    filter(item=="1")%>%
    select(-1)
  
  dat_trainee_autonomy <- read.csv(paste("PGY2_", n,".csv",sep=""))%>%
    filter(rater=="2")%>%
    filter(item=="1")%>%
    select(-1)
  
  dat_faculty_performance <- read.csv(paste("PGY2_", n,".csv",sep=""))%>%
    filter(rater=="1")%>%
    filter(item=="2")%>%
    select(-1)
  
  dat_trainee_performance <- read.csv(paste("PGY2_", n,".csv",sep=""))%>%
    filter(rater=="2")%>%
    filter(item=="2")%>%
    select(-1)
  
  autonomy <- merge(dat_faculty_autonomy,dat_trainee_autonomy,
                    by.x = c("procID","subjectID"),by.y = c("procID","subjectID")) %>%
    select("procID","subjectID","score.x","score.y") 
  
  performance <- merge(dat_faculty_performance,dat_trainee_performance,
                       by.x = c("procID","subjectID"),by.y = c("procID","subjectID")) %>%
    select("procID","subjectID","score.x","score.y") 
  
  autonomy_faculty <- autonomy %>%
    select("procID","subjectID","score.x") %>%
    spread(procID,score.x) %>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  autonomy_trainee <- autonomy %>%
    select("procID","subjectID","score.y") %>%
    spread(procID,score.y)%>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  performance_faculty <- performance %>%
    select("procID","subjectID","score.x") %>%
    spread(procID,score.x)%>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  performance_trainee <- performance %>%
    select("procID","subjectID","score.y") %>%
    spread(procID,score.y)%>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  
  autonomy.relia.faculty <- alpha(autonomy_faculty[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  autonomy.relia.trainee <- alpha(autonomy_trainee[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  autonomy.correlation <- cor(autonomy$score.x,autonomy$score.y)
  PGY2_C[n,1] <- autonomy.correlation/sqrt(autonomy.relia.faculty*autonomy.relia.trainee)
  performance.relia.faculty <- alpha(performance_faculty[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  performance.relia.trainee <- alpha(performance_trainee[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  performance.correlation <- cor(performance$score.x,performance$score.y)
  PGY2_C[n,2] <- performance.correlation/sqrt(performance.relia.faculty*performance.relia.trainee)
  PGY2_C[n,3] <- mean(autonomy$score.x)
  PGY2_C[n,4] <- mean(autonomy$score.y)
  PGY2_C[n,5] <- mean(performance$score.x)
  PGY2_C[n,6] <- mean(performance$score.y)
}

##setwd("C:/XXX")
colnames(PGY2_C)<- c("Autonomy Cor","Performance Cor","Mean Faculty Autonomy", "Mean Trainee Autonomy","Mean Faculty Performance","Mean Trainee Performance")
write.csv(PGY2_C, "PGY2_C.csv")

################################################################################
#####################  distennuated correlation PGY 3  #########################
################################################################################
PGY3_C<- matrix(ncol = 6, nrow = 18)
for (n in 1:18)
{
  ###setwd("C:/XXX")
  
  dat_faculty_autonomy <- read.csv(paste("PGY3_", n,".csv",sep=""))%>%
    filter(rater=="1")%>%
    filter(item=="1")%>%
    select(-1)
  
  dat_trainee_autonomy <- read.csv(paste("PGY3_", n,".csv",sep=""))%>%
    filter(rater=="2")%>%
    filter(item=="1")%>%
    select(-1)
  
  dat_faculty_performance <- read.csv(paste("PGY3_", n,".csv",sep=""))%>%
    filter(rater=="1")%>%
    filter(item=="2")%>%
    select(-1)
  
  dat_trainee_performance <- read.csv(paste("PGY3_", n,".csv",sep=""))%>%
    filter(rater=="2")%>%
    filter(item=="2")%>%
    select(-1)
  
  autonomy <- merge(dat_faculty_autonomy,dat_trainee_autonomy,
                    by.x = c("procID","subjectID"),by.y = c("procID","subjectID")) %>%
    select("procID","subjectID","score.x","score.y") 
  
  performance <- merge(dat_faculty_performance,dat_trainee_performance,
                       by.x = c("procID","subjectID"),by.y = c("procID","subjectID")) %>%
    select("procID","subjectID","score.x","score.y") 
  
  autonomy_faculty <- autonomy %>%
    select("procID","subjectID","score.x") %>%
    spread(procID,score.x) %>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  autonomy_trainee <- autonomy %>%
    select("procID","subjectID","score.y") %>%
    spread(procID,score.y)%>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  performance_faculty <- performance %>%
    select("procID","subjectID","score.x") %>%
    spread(procID,score.x)%>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  performance_trainee <- performance %>%
    select("procID","subjectID","score.y") %>%
    spread(procID,score.y)%>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  
  autonomy.relia.faculty <- alpha(autonomy_faculty[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  autonomy.relia.trainee <- alpha(autonomy_trainee[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  autonomy.correlation <- cor(autonomy$score.x,autonomy$score.y)
  PGY3_C[n,1] <- autonomy.correlation/sqrt(autonomy.relia.faculty*autonomy.relia.trainee)
  performance.relia.faculty <- alpha(performance_faculty[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  performance.relia.trainee <- alpha(performance_trainee[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  performance.correlation <- cor(performance$score.x,performance$score.y)
  PGY3_C[n,2] <- performance.correlation/sqrt(performance.relia.faculty*performance.relia.trainee)
  PGY3_C[n,3] <- mean(autonomy$score.x)
  PGY3_C[n,4] <- mean(autonomy$score.y)
  PGY3_C[n,5] <- mean(performance$score.x)
  PGY3_C[n,6] <- mean(performance$score.y)
}
###setwd("C:/XXX")
colnames(PGY3_C)<- c("Autonomy Cor","Performance Cor","Mean Faculty Autonomy", "Mean Trainee Autonomy","Mean Faculty Performance","Mean Trainee Performance")
write.csv(PGY3_C, "PGY3_C.csv")

################################################################################
#####################  distennuated correlation PGY 4  #########################
################################################################################
PGY4_C<- matrix(ncol = 6, nrow = 18)
for (n in 1:18)
{
  ###setwd("XXX")
  
  dat_faculty_autonomy <- read.csv(paste("PGY4_", n,".csv",sep=""))%>%
    filter(rater=="1")%>%
    filter(item=="1")%>%
    select(-1)
  
  dat_trainee_autonomy <- read.csv(paste("PGY4_", n,".csv",sep=""))%>%
    filter(rater=="2")%>%
    filter(item=="1")%>%
    select(-1)
  
  dat_faculty_performance <- read.csv(paste("PGY4_", n,".csv",sep=""))%>%
    filter(rater=="1")%>%
    filter(item=="2")%>%
    select(-1)
  
  dat_trainee_performance <- read.csv(paste("PGY4_", n,".csv",sep=""))%>%
    filter(rater=="2")%>%
    filter(item=="2")%>%
    select(-1)
  
  autonomy <- merge(dat_faculty_autonomy,dat_trainee_autonomy,
                    by.x = c("procID","subjectID"),by.y = c("procID","subjectID")) %>%
    select("procID","subjectID","score.x","score.y") 
  
  performance <- merge(dat_faculty_performance,dat_trainee_performance,
                       by.x = c("procID","subjectID"),by.y = c("procID","subjectID")) %>%
    select("procID","subjectID","score.x","score.y") 
  
  autonomy_faculty <- autonomy %>%
    select("procID","subjectID","score.x") %>%
    spread(procID,score.x) %>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  autonomy_trainee <- autonomy %>%
    select("procID","subjectID","score.y") %>%
    spread(procID,score.y)%>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  performance_faculty <- performance %>%
    select("procID","subjectID","score.x") %>%
    spread(procID,score.x)%>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  performance_trainee <- performance %>%
    select("procID","subjectID","score.y") %>%
    spread(procID,score.y)%>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  
  autonomy.relia.faculty <- alpha(autonomy_faculty[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  autonomy.relia.trainee <- alpha(autonomy_trainee[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  autonomy.correlation <- cor(autonomy$score.x,autonomy$score.y)
  PGY4_C[n,1] <- autonomy.correlation/sqrt(autonomy.relia.faculty*autonomy.relia.trainee)
  performance.relia.faculty <- alpha(performance_faculty[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  performance.relia.trainee <- alpha(performance_trainee[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  performance.correlation <- cor(performance$score.x,performance$score.y)
  PGY4_C[n,2] <- performance.correlation/sqrt(performance.relia.faculty*performance.relia.trainee)
  PGY4_C[n,3] <- mean(autonomy$score.x)
  PGY4_C[n,4] <- mean(autonomy$score.y)
  PGY4_C[n,5] <- mean(performance$score.x)
  PGY4_C[n,6] <- mean(performance$score.y)
}
###setwd("XXX")
colnames(PGY4_C)<- c("Autonomy Cor","Performance Cor","Mean Faculty Autonomy", "Mean Trainee Autonomy","Mean Faculty Performance","Mean Trainee Performance")
write.csv(PGY4_C, "PGY4_C.csv")

################################################################################
#####################  distennuated correlation PGY 5  #########################
################################################################################
PGY5_C<- matrix(ncol = 6, nrow = 32)
for (n in 1:32)
{
  ###setwd("XXX")
  
  dat_faculty_autonomy <- read.csv(paste("PGY5_", n,".csv",sep=""))%>%
    filter(rater=="1")%>%
    filter(item=="1")%>%
    select(-1)
  
  dat_trainee_autonomy <- read.csv(paste("PGY5_", n,".csv",sep=""))%>%
    filter(rater=="2")%>%
    filter(item=="1")%>%
    select(-1)
  
  dat_faculty_performance <- read.csv(paste("PGY5_", n,".csv",sep=""))%>%
    filter(rater=="1")%>%
    filter(item=="2")%>%
    select(-1)
  
  dat_trainee_performance <- read.csv(paste("PGY5_", n,".csv",sep=""))%>%
    filter(rater=="2")%>%
    filter(item=="2")%>%
    select(-1)
  
  autonomy <- merge(dat_faculty_autonomy,dat_trainee_autonomy,
                    by.x = c("procID","subjectID"),by.y = c("procID","subjectID")) %>%
    select("procID","subjectID","score.x","score.y") 
  
  performance <- merge(dat_faculty_performance,dat_trainee_performance,
                       by.x = c("procID","subjectID"),by.y = c("procID","subjectID")) %>%
    select("procID","subjectID","score.x","score.y") 
  
  autonomy_faculty <- autonomy %>%
    select("procID","subjectID","score.x") %>%
    spread(procID,score.x) %>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  autonomy_trainee <- autonomy %>%
    select("procID","subjectID","score.y") %>%
    spread(procID,score.y)%>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  performance_faculty <- performance %>%
    select("procID","subjectID","score.x") %>%
    spread(procID,score.x)%>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  performance_trainee <- performance %>%
    select("procID","subjectID","score.y") %>%
    spread(procID,score.y)%>%
    dplyr::rename(item1=2,item2=3, item3=4)
  
  
  autonomy.relia.faculty <- alpha(autonomy_faculty[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  autonomy.relia.trainee <- alpha(autonomy_trainee[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  autonomy.correlation <- cor(autonomy$score.x,autonomy$score.y)
  PGY5_C[n,1] <- autonomy.correlation/sqrt(autonomy.relia.faculty*autonomy.relia.trainee)
  performance.relia.faculty <- alpha(performance_faculty[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  performance.relia.trainee <- alpha(performance_trainee[,c("item1","item2","item3")],check.keys=TRUE)$total[1,1]
  performance.correlation <- cor(performance$score.x,performance$score.y)
  PGY5_C[n,2] <- performance.correlation/sqrt(performance.relia.faculty*performance.relia.trainee)
  PGY5_C[n,3] <- mean(autonomy$score.x)
  PGY5_C[n,4] <- mean(autonomy$score.y)
  PGY5_C[n,5] <- mean(performance$score.x)
  PGY5_C[n,6] <- mean(performance$score.y)
}
###setwd("XXX")
colnames(PGY5_C)<- c("Autonomy Cor","Performance Cor","Mean Faculty Autonomy", "Mean Trainee Autonomy","Mean Faculty Performance","Mean Trainee Performance")
write.csv(PGY5_C, "PGY5_C.csv")









